/**
 * 拜访记录 - 详情页面
 * @author miaoyl
 * @date 2019/04/22
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table,
        vueData = {
            form: {
                TYPE: '1',
                GWAKL004:'',
                GWBGL003:'',
                GWAVL003:'',
                GWBH002:'',
                GWBHORIG:'',
                GWBH031:''
            },
            dto:[]
        },
        table_where1 = {
            GWBHNAME: '',
            GWBHID: '',
            GWBHSEQ2: '',
            TYPE: ''
        },
        table_where2 = {
            GWCS010: ''
        },
        cols1 = [[
             {field: 'GWBH003', width: 80, title: '拜访日期', totalRowText: '合计'}
            , {field: 'GWBKSEQ2', width: 240, title: '项次'}
            , {field: 'GWBK001', width: 80, title: '商品编号'}
            , {field: 'GWAML003', width: 80, title: '品名'}
            , {field: 'GWAML004', width: 80, title: '规格'}
            , {field: 'GWBK003', width: 80, title: '数量'}
            , {field: 'GWBK004', width: 80, title: '单价'}
            , {field: 'GWBK005', width: 80, title: '金额', totalRow: true}
            , {field: 'GWBK008', width: 140, title: '预计购买日期'}
            , {field: 'GWBK009', width: 140, title: '预计回款日期'}
            , {field: 'GWBK006', width: 80, title: '折扣率', hide:true}
            , {field: 'GWBK007', width: 80, title: '折扣金额', hide:true}
        ]],
        cols2 = [[
             {field: 'GWCSDOCNO', width: 160, title: '成交单号', totalRowText: '合计'}
            , {field: 'GWCSSEQ', width: 160, title: '项次'}
            , {field: 'GWCS001', width: 160, title: '商品编号'}
            , {field: 'GWAML003', width: 160, title: '品名'}
            , {field: 'GWAML004', width: 160, title: '规格'}
            , {field: 'GWCS003', width: 160, title: '数量'}
            , {field: 'GWCS004', width: 160, title: '单价'}
            , {field: 'GWCS005', width: 160, title: '金额', totalRow: true}
            , {field: 'GWCS035', width: 160, title: '折扣率'}
            , {field: 'GWCS036', width: 160, title: '折扣金额'}
            , {field: 'GWCSDOCDATE', width: 160, title: '实际购买日期'}
        ]],
        gwatTable1,
        gwatTable2,
        callObj
    ;

    var Gwat160Detail = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwat160_detail'
            }, win);
        },

        initTable1: function () {
            layui.use(['table'], function () {
                table = layui.table;
                gwatTable1 = table.render({
                    elem: app.getByid(doc, 'gwat160_detail_table1'),
                    url: g_ctxPath + 'crm/gwat160/getGwbkData',
                    id: 'gwat160_detail_table1',
                    totalRow: true,
                    height: 200,
                    cols: cols1,
                    loading: true,
                    page: true,
                    where: table_where1
                });

            });
        },

        initTable2: function () {
            layui.use(['table'], function () {
                table = layui.table;
                gwatTable2 = table.render({
                    elem: app.getByid(doc, 'gwat160_detail_table2'),
                    url: g_ctxPath + 'crm/gwat160/getGwcsData',
                    id: 'gwat160_detail_table2',
                    totalRow: true,
                    cols: cols2,
                    height: 250,
                    loading: true,
                    page: true,
                    where: table_where1
                });

            });
        },

        table1Reload:function (table_where1) {
            self._reloadTable({id: 'gwat160_detail_table1', where: table_where1});
        },

        table2Reload:function () {
            self._reloadTable({id: 'gwat160_detail_table2', where: table_where2});
        },

        _call: function (tagData) {
            callObj = tagData.obj;

            app.ajax({
                xurl: 'crm/gwat160/getDetailData',
                xdata: app.cloneJSON(tagData.obj),
                cb: function (result) {
                    if (result.ok) {
                        app.extendUpLow({
                            target: vueData.form,
                            res: result.data.data,
                            reset: true
                        });
                        vueData.dto = result.data.data.dto;
                    }
                }
            });

            vueData.form.GWBGNAME = callObj.GWBHNAME;
            vueData.form.GWBJID = callObj.GWBHID;
            vueData.form.GWBJSEQ = callObj.GWBHSEQ;
            table_where1.GWBHNAME = callObj.GWBHNAME;
            table_where1.GWBHID = callObj.GWBHID;
            table_where1.GWBHSEQ2 = callObj.GWBHSEQ;
            table_where1.TYPE = '1';
            self.table1Reload(table_where1);

            table_where2.GWCS010 = callObj.GWBHID;
            self.table2Reload();
        },

        _ready: function () {
            //vueData.form = app.fields_(['GWBJ_T'],vueData.form);
            var vue = new Vue({
                el: '#gwat160_detail',
                data: vueData,
                methods: {
                    cancel: function () {
                        self._closeTpl();
                    },
                    queryGwbk:function (data) {
                        table_where1.GWBHNAME = vueData.form.GWBHNAME;
                        table_where1.GWBHID = vueData.form.GWBHID;
                        table_where1.GWBHSEQ2 = vueData.form.GWBHSEQ;
                        table_where1.TYPE = data;
                        self.table1Reload(table_where1);

                        table_where2.GWCS010 = vueData.form.GWBHID;
                        self.table2Reload();
                    }
                },
                watch: {},
                created: function () {
                    self._setEleIsReady('gwat160_detail');
                }
            });

            self.initTable1();
            self.initTable2();

            //收缩详情
            $(doc).on('click','.more',function () {
                var _that = this;
                var mParent = _that.parentNode.lastChild;
                if (this.innerText == '详情') {
                    mParent.style.height = 'inherit';
                    mParent.classList.remove('textMore');
                    this.innerText = '收缩';
                } else {
                    mParent.style.height = '23px';
                    mParent.classList.add('textMore');
                    this.innerText = '详情';
                }
            });
        },

        _lazy: function () {
            this._super();
        }
    });

    return new Gwat160Detail();

});